import BasePool from "../../db/BasePool";

export default class systemModel extends BasePool {

	/**
	 * --------------------------------------用户管理---------------------------------------
	 **/
	
	// 查询用户列表
	async getUserList(params){
		let limit = ``;
		let parameter:any = [];
		let search = ``;
		// 搜索
		if(params.name){
			let name = params.name.trim();
			search += ` and u.name like CONCAT(?,'%') `;
			parameter.push(name);
		}
		if(params.companyid){
			search += ` and u.companyid = ? `;
			parameter.push(params.companyid);
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql = `SELECT u.id, u.telphone, u.name, u.loginuser, u.description, u.cnum, u.level, u.roleid, u.companyid, u.iscompanymanager, u.createAt, u.status, u.roleid, u.loginTime, r.name roleName FROM tbl_user u LEFT JOIN tbl_role r ON u.roleid = r.id AND r. STATUS = 1 WHERE u. STATUS != 0 ${search} ORDER BY u.createAt DESC ${limit}`;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 查询用户数量
	async getUserCount(params){
		let search = "";
		let parameter:any = [];
		if(params.name){
			let name = params.name.trim();
			search = ` and name like CONCAT(?,'%') `;
			parameter.push(name);
		}
		if(params.companyid){
			search += ` and companyid = ? `;
			parameter.push(params.companyid);
		}
		let sql="select count(*) c from tbl_user where status != 0"+search;
		let res:any = await this.query(sql,parameter);
		return res[0] ? res[0].c : 0;
	}
	// 查询账号是否存在
	async getUserByAccount(obj){
		let sql="select * from tbl_user where status!=0 and loginuser=? || telphone=?";
		if(obj.id){
			sql += " and id != ? "
		}
		return await this.query(sql,[obj.loginuser,obj.telphone,obj.id]);
	}
	// 编辑用户
	async editUser(obj){
		let sql="update tbl_user set ? where id=?";
		return await this.query(sql,[obj,obj.id]);
	}
	// 添加用户
	async saveUser(obj){
		let sql="insert into tbl_user set ? ";
		return await this.query(sql,obj);
	}
	// 禁用or启用
	async delUser(obj){
		let sql="update tbl_user set status=? where id=?";
		return await this.query(sql,[obj.status,obj.id]);
	}
	// 生成授权码
	async createCode(obj){
		let sql="insert into tbl_code set ? ";
		return await this.query(sql,obj);
	}
	
	

	/**
	 * ------------------------------------------角色管理---------------------------------------------
	 **/
	
	// 查询角色列表
	async getRoleList(params){
		let limit = ``;
		let parameter:any = [];
		let search = "";
		// 搜索
		if(params.name){
			let name = params.name.trim();
			search = " and name like CONCAT(?,'%') "
			parameter.push(name);
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql=`select * from tbl_role where status=1 ${search} order by createAt desc ${limit}`;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 查询角色数量
	async getRoleCount(params){
		let search = "";
		let name = "";
		if(params.name){
			name = params.name.trim();
			search = ` and name like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_role where status = 1"+search;
		let res:any = await this.queryOne(sql,name);
		return res ? res.c : 0;
	}
	// 角色列表不分页
	async getRoleListNopage(){
		let sql=`select * from tbl_role where status=1 order by createAt desc`;
		let res = await this.query(sql);
		return res;
	}
	// 编辑角色
	async editRole(obj){
		let sql="update tbl_role set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加角色
	async saveRole(obj){
		let sql="insert into tbl_role set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 保存菜单权限
	async saveRoleMenu(obj){
		let sql="insert into tbl_role_menu set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 删除该角色之前的菜单权限
	async delRoleMenu(roleid){
		let sql="update tbl_role_menu set status=0 where roleid=?";
		let res = await this.query(sql,roleid);
		return res;
	}
	// 删除角色
	async delRole(id){
		let sql="update tbl_role set status=0 where id=?";
		let res = await this.query(sql,id);
		return res;
	}
	// 查询某个角色权限
	async getMenuRole(params){
		let sql = `SELECT rm.*, m.pid FROM tbl_role_menu rm, tbl_menu m WHERE rm.menuid = m.id AND rm.status = 1 AND m.status = 1 AND rm.roleid = ?`
	  let res:any = await this.query(sql,params.id);
	  return res;
	}
	
	/**
	 * ----------------------------------------------菜单管理----------------------------------------------
	 **/
	
	// 菜单列表
	async getMenuList(){
	  let sql="select * from tbl_menu where status=1 order by sort";
	  let res:any = await this.query(sql);
	  return res;
	}
	// 添加菜单
	async saveMenu(obj){
		let sql="insert into tbl_menu set ?";
		let res = await this.query(sql,obj);
		return res;
	}
	// 添加菜单meta
	async saveMenuMeta(obj){
		let sql="insert into tbl_menu_meta set ?";
		let res = await this.query(sql,obj);
		return res;
	}
	// 编辑菜单
	async editMenu(obj){
		let sql="update tbl_menu set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 编辑菜单meta
	async editMenuMeta(obj){
		let sql="update tbl_menu_meta set ? where menuid=?";
		let res = await this.query(sql,[obj,obj.menuid]);
		return res;
	}
	// 删除菜单
	async delMenu(id){
		let sql="UPDATE tbl_menu m, tbl_menu_meta mm SET m. STATUS = 0, mm. STATUS = 0 WHERE m.id = mm.menuid AND m.id =?";
		let res = await this.query(sql,id);
		return res;
	}



	/**
	 * -----------------------------------------------一级代理商-----------------------------------------------
	 **/

	// 列表
	async getCompanyList(params){
		let limit = ``;
		let search = "";
		let parameter:any = [];
		if(params.companyname){
			let name = params.companyname.trim();
			search = " and companyname like CONCAT(?,'%') "
			parameter.push(name)
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql="select * from tbl_company where status=1 "+search+" order by createAt desc"+limit;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 数量
	async getCompanyCount(params){
		let search = "";
		let name = "";
		if(params.companyname){
			name = params.companyname.trim();
			search = ` and companyname like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_company where status = 1"+search;
		let res:any = await this.queryOne(sql,name);
		return res ? res.c : 0;
	}
	// 编辑
	async editCompany(obj){
		let sql="update tbl_company set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加
	async saveCompany(obj){
		let sql="insert into tbl_company set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 删除
	async delCompany(id){
		let sql="update tbl_company set status=0 where id=?";
		let res = await this.query(sql,id);
		return res;
	}



	/**
	 * -----------------------------------------------授权码-----------------------------------------------
	 **/
	// 列表
	async getCodeList(params){
		let limit = ``;
		let search = "";
		let parameter:any = [params.telphone];
		if(params.userid){
			let name = params.userid.trim();
			search = " and userid like CONCAT(?,'%') "
			parameter.push(name)
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql="select * from tbl_code where status=1 and userid = ? "+search+" order by createAt desc"+limit;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 数量
	async getCodeCount(params){
		let search = "";
		let name = "";
		if(params.userid){
			name = params.userid.trim();
			search = ` and userid like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_code where status = 1 and userid = ? "+search;
		let res:any = await this.queryOne(sql,[params.telphone,name]);
		return res ? res.c : 0;
	}


	/**
	 * -----------------------------------------------电脑主机管理-----------------------------------------------
	 **/

	// 列表
	async getComputerList(params){
		let limit = ``;
		let search = "";
		let parameter:any = params.companyid ? [params.companyid] : [];
		if(params.telephone){
			let telephone = params.telephone.trim();
			search = " and userid like CONCAT(?,'%') "
			parameter.push(telephone)
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql="select * from tbl_comput where status!=0 "+search+" order by createAt desc"+limit;
		// 判断是否是一级代理商
		if(params.companyid){
			sql="select c.* from tbl_comput c, tbl_user u where c.status!=0 and c.userid = u.telphone and u.companyid = ?"+search+" order by createAt desc"+limit;
		}
		let res = await this.query(sql,parameter);
		return res;
	}
	// 数量
	async getComputerCount(params){
		let search = "";
		let parameter:any = params.companyid ? [params.companyid] : [];
		if(params.telephone){
			let telephone = params.telephone.trim();
			search = ` and userid like CONCAT(?,'%') `;
			parameter.push(telephone)
		}
		let sql="select count(*) c from tbl_comput where status = 1"+search;
		// 判断是否是一级代理商
		if(params.companyid){
			sql="select c.* from tbl_comput c, tbl_user u where c.status=1 and c.userid = u.telphone and u.companyid = ?"+search;
		}
		let res:any = await this.queryOne(sql,parameter);
		return res ? res.c : 0;
	}
	// 审核
	async examineComputer(params){
		let sql="update tbl_comput set status=1 where id in (?)";
		return await this.query(sql,params.ids.join(','));
	}
	// 延期
	async extensionComputer(params){
		let {extension,type,selectIds} = params
		let ids = selectIds.map(item => `'${item}'`).join(',')
		let sql=`update tbl_comput set lastendtime=endtime,endtime = date_add(if(ISNULL(endtime),now(),endtime),interval ? ${type}) WHERE id in (${ids})`;
		return await this.query(sql,extension);
	}


	
	/**
	 * -----------------------------------------------账号管理-----------------------------------------------
	 **/

	// 列表
	async getAccountList(params){
		let limit = ``;
		let search = "";
		let parameter:any = [];
		if(params.accoutname){
			let name = params.accoutname.trim();
			search = " and accoutname like CONCAT(?,'%') "
			parameter.push(name)
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql="select * from tbl_account where status=1 "+search+" order by createAt desc"+limit;
		return await this.query(sql,parameter);
	}
	// 数量
	async getAccountCount(params){
		let search = "";
		let name = "";
		if(params.accoutname){
			name = params.accoutname.trim();
			search = ` and accoutname like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_account where status = 1"+search;
		let res:any = await this.queryOne(sql,name);
		return res ? res.c : 0;
	}
	// 编辑
	async editAccount(obj){
		let sql="update tbl_account set ? where id=?";
		return await this.query(sql,[obj,obj.id]);
	}
	// 添加
	async saveAccount(obj){
		let sql="insert into tbl_account set ? ";
		return await this.query(sql,obj);
	}
	// 删除
	async delAccount(id){
		let sql="update tbl_account set status=0 where id=?";
		return await this.query(sql,id);
	}


	/**
	 * -----------------------------------------------ip代理管理-----------------------------------------------
	 **/

	// 列表
	async getIpProxyList(params){
		let limit = ``;
		let search = "";
		let parameter:any = [];
		if(params.accoutname){
			let name = params.accoutname.trim();
			search = " and accoutname like CONCAT(?,'%') "
			parameter.push(name)
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql="select * from tbl_proxyip where status=1 "+search+" order by createAt desc"+limit;
		let res = await this.query(sql,parameter);
		return res;
	}
	// 数量
	async getIpProxyCount(params){
		let search = "";
		let name = "";
		if(params.accoutname){
			name = params.accoutname.trim();
			search = ` and accoutname like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_proxyip where status = 1"+search;
		let res:any = await this.queryOne(sql,name);
		return res ? res.c : 0;
	}
	// 编辑
	async editIpProxy(obj){
		let sql="update tbl_proxyip set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加
	async saveIpProxy(obj){
		let sql="insert into tbl_proxyip set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 删除
	async delIpProxy(id){
		let sql="update tbl_proxyip set status=0 where id=?";
		let res = await this.query(sql,id);
		return res;
	}


	/**
	 * -----------------------------------------------任务管理-----------------------------------------------
	 **/

	// 列表
	async getTaskList(params){
		let limit = ``;
		let search = "";
		let parameter:any = [];
		if(params.name){
			let name = params.name.trim();
			search = " and t.name like CONCAT(?,'%') "
			parameter.push(name)
		}
		// 分页
		if(params.pageNum && params.pageSize){
			let pageNumber = (params.pageNum-1)*params.pageSize;
			limit = ` LIMIT ?,? `;
			parameter.push(pageNumber,params.pageSize*1);
		}
		let sql = "SELECT t.*, c.companyname, l.name laborunionname FROM tbl_task t LEFT JOIN tbl_company c ON t.compayid = c.id LEFT JOIN tbl_laborunion l ON t.laborunionid = l.id WHERE t.STATUS = 1 "+search+" order by t.createAt desc"+limit
		return await this.query(sql,parameter);
	}
	// 数量
	async getTaskCount(params){
		let search = "";
		let name = "";
		if(params.name){
			name = params.name.trim();
			search = ` and t.name like CONCAT(?,'%') `;
		}
		let sql="select count(*) c from tbl_task where status = 1"+search;
		let res:any = await this.queryOne(sql,name);
		return res ? res.c : 0;
	}
	// 编辑
	async editTask(obj){
		let sql="update tbl_task set ? where id=?";
		let res = await this.query(sql,[obj,obj.id]);
		return res;
	}
	// 添加
	async saveTask(obj){
		let sql="insert into tbl_task set ? ";
		let res = await this.query(sql,obj);
		return res;
	}
	// 删除
	async delTask(id){
		let sql="update tbl_task set status=0 where id=?";
		let res = await this.query(sql,id);
		return res;
	}


	/**
	 * -----------------------------------------------工会管理-----------------------------------------------
	 **/

	// 列表，不分页，查全部
	async getLaborunionListNopage(){
		let sql="select * from tbl_laborunion where status=1 order by createAt desc";
		let res = await this.query(sql);
		return res;
	}


}
